package com.example.administrator.boltshow;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import adapter.SpecialAdapter;
import adapter.SpecialClassifyAdapter;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;
import config.NetConfig;
import model.HotMonkeyModel;
import model.SpecialClassifyModel;

public class SpecialActivity extends AppCompatActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {

    private ImageView iv_show,iv_share;
    private TextView tv_title,tv_have,tv_classify,tv_order;
    private RecyclerView mRecyclerView;
    private List<HotMonkeyModel.DataBean> mData;
    private SpecialAdapter mAdapter;
    private GridLayoutManager mManager;
    private Boolean flag = false;
    private PopupWindow mPopupWindow,mPopupWindowOrder;
    private RecyclerView mRecyclerViewSex;
    private RecyclerView mRecyclerViewCC;
    private List<SpecialClassifyModel.DataBean.AttrBean> mClassifyData;
    private SpecialClassifyAdapter mClassifyAdapter,mSexAdapter;
    private TextView mTVSex,mTVCc;
    private List<SpecialClassifyModel.DataBean.AttrBean> mSexData;
    private TextView mTextViewClose;
    private Button mButtonSucceed;
    private String specialId1;
    private ImageView mImageViewClose;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_special);

        initView();
        initData();
        setData();
        setListener();

    }

    private void setData() {
        mManager = new GridLayoutManager(this,2, GridLayoutManager.VERTICAL,false);
        mRecyclerView.setLayoutManager(mManager);
        mRecyclerView.setAdapter(mAdapter);
        mPopupWindow.setAnimationStyle(R.style.take_photo_anim);

        GridLayoutManager managerCc = new GridLayoutManager(this,3, GridLayoutManager.VERTICAL,false);
        mRecyclerViewCC.setLayoutManager(managerCc);
        mRecyclerViewCC.setAdapter(mClassifyAdapter);

        GridLayoutManager managerSex = new GridLayoutManager(this,3, GridLayoutManager.VERTICAL,false);
        mRecyclerViewSex.setLayoutManager(managerSex);
        mRecyclerViewSex.setAdapter(mSexAdapter);
    }

    private void setListener() {
        tv_have.setOnClickListener(this);
        iv_share.setOnClickListener(this);
        tv_classify.setOnClickListener(this);
        mTextViewClose.setOnClickListener(this);
        mButtonSucceed.setOnClickListener(this);
        mImageViewClose.setOnClickListener(this);
        tv_order.setOnClickListener(this);
    }

    private void initData() {
        mClassifyData = new ArrayList<>();
        mSexData = new ArrayList<>();
        mData = new ArrayList<>();
        mClassifyAdapter = new SpecialClassifyAdapter(mClassifyData,this,this);
        mSexAdapter = new SpecialClassifyAdapter(mSexData,this,this);

        Intent intent = getIntent();
        HotMonkeyModel hotModel = (HotMonkeyModel) intent.getSerializableExtra("model");
        tv_title.setText(hotModel.getTitle());
        x.image().bind(iv_show,hotModel.getTopImg());
        mData.addAll(hotModel.getData());
        specialId1 = hotModel.getSpecialId();
        mAdapter = new SpecialAdapter(mData,this,specialId1);

        final String specialId = hotModel.getSpecialId();
        String classifyPath = String.format(NetConfig.HOT_SELECT,specialId);
        x.http().get(new RequestParams(classifyPath), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                SpecialClassifyModel specialClassifyModel = gson.fromJson(result, SpecialClassifyModel.class);

                mSexData.addAll(specialClassifyModel.getData().get(0).getAttr());
                mSexAdapter.notifyDataSetChanged();

                mClassifyData.addAll(specialClassifyModel.getData().get(1).getAttr());
                mClassifyAdapter.notifyDataSetChanged();

                mTVSex.setText(specialClassifyModel.getData().get(0).getName());
                mTVCc.setText(specialClassifyModel.getData().get(1).getName());


            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });



    }

    private void initView() {
        mRecyclerView = (RecyclerView) findViewById(R.id.rv_special_content);
        iv_show = (ImageView) findViewById(R.id.iv_special_show);
        tv_title = (TextView) findViewById(R.id.tv_special_title);
        tv_have = (TextView) findViewById(R.id.tv_special_have);
        tv_classify = (TextView) findViewById(R.id.tv_special_classify);
        tv_order = (TextView) findViewById(R.id.tv_special_order);
        iv_share = (ImageView) findViewById(R.id.iv_special_share);
        mImageViewClose = (ImageView) findViewById(R.id.iv_special_close);

        View contentView = LayoutInflater.from(this).inflate(R.layout.pop_window_special, null);
        mPopupWindow = new PopupWindow(contentView, ViewPager.LayoutParams.MATCH_PARENT, ViewPager.LayoutParams.MATCH_PARENT, true);
        mPopupWindow.setTouchable(true);
        mPopupWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;
            }
        });
        mPopupWindow.setBackgroundDrawable(getResources().getDrawable(R.color.white));
        mRecyclerViewSex = (RecyclerView) contentView.findViewById(R.id.rv_pop_special_sex);
        mRecyclerViewCC = (RecyclerView) contentView.findViewById(R.id.rv_pop_special_cc);
        mTVSex = (TextView) contentView.findViewById(R.id.tv_pop_special_sex);
        mTVCc = (TextView) contentView.findViewById(R.id.tv_pop_special_cc);
        mTextViewClose = (TextView) contentView.findViewById(R.id.tv_special_pop_close);
        mButtonSucceed = (Button) contentView.findViewById(R.id.bt_special_pop_succeed);

        View contentOrder = LayoutInflater.from(this).inflate(R.layout.special_pop_window_order,null);
        mPopupWindowOrder = new PopupWindow(contentOrder, ViewPager.LayoutParams.MATCH_PARENT, 600, true);
        mPopupWindowOrder.setTouchable(true);
        mPopupWindowOrder.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;
            }
        });
        mPopupWindowOrder.setBackgroundDrawable(getResources().getDrawable(R.color.white));
        RadioGroup mRadioGroup = (RadioGroup) contentOrder.findViewById(R.id.rg_special_pop_order);
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.tv_special_order_moren :
                        tv_order.setText("默认排序 ");
                        mPopupWindowOrder.dismiss();
                        break;
                    case R.id.tv_special_order_current :
                        tv_order.setText("价格低到高 ");
                        mPopupWindowOrder.dismiss();
                        break;
                    case R.id.tv_special_order_three :
                        tv_order.setText("价格高到低 ");
                        mPopupWindowOrder.dismiss();
                        break;
                    case R.id.tv_special_order_four :
                        tv_order.setText("人气高到低 ");
                        mPopupWindowOrder.dismiss();
                        break;
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        if(v!=null) {
            switch (v.getId()) {
                case R.id.tv_special_have :
                    Drawable drawable = null;
                    if(flag) {
                        drawable = getResources().getDrawable(R.mipmap.login_password_close);
                    }else if(!flag) {
                        drawable = getResources().getDrawable(R.mipmap.login_password_open);
                    }
                    if(drawable!=null) {
                        drawable.setBounds(0,0,drawable.getMinimumWidth(),drawable.getMinimumHeight());
                        tv_have.setCompoundDrawables(drawable,null,null,null);
                        flag = !flag;
                    }
                    break;
                case R.id.iv_special_share :
                    showShare();
                    break;
                case R.id.tv_special_classify :
                    mPopupWindow.showAtLocation(v, Gravity.BOTTOM,0,0);
                    break;
                case R.id.tv_special_pop_close :
                    mPopupWindow.dismiss();
                    break;
                case R.id.bt_special_pop_succeed:
                    mPopupWindow.dismiss();
                    break;
                case R.id.iv_special_close :
                    finish();
                    break;
                case R.id.tv_special_order :
                    mPopupWindowOrder.showAsDropDown(v);
                    break;

            }
        }

    }

    private void showShare() {
        ShareSDK.initSDK(this);
        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();

        // 分享时Notification的图标和文字  2.5.9以后的版本不调用此方法
        //oks.setNotification(R.drawable.ic_launcher, getString(R.string.app_name));
        // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
        oks.setTitle(getString(R.string.share));
        // titleUrl是标题的网络链接，仅在人人网和QQ空间使用
        oks.setTitleUrl("http://sharesdk.cn");
        // text是分享文本，所有平台都需要这个字段
        oks.setText("我是分享文本");
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
        //oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
        // url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl("http://sharesdk.cn");
        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
        oks.setComment("我是测试评论文本");
        // site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite(getString(R.string.app_name));
        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl("http://sharesdk.cn");

        // 启动分享GUI
        oks.show(this);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

    }
}
